#include "exampleleastr.h"

void runLeastR(){
	int m,n,state;
	double *A, *xorin, *noise, *y, rho;
	OPTS opt;
	double c1,c2;
	stopWatch sw;
	FUNVAL result1,result2,result3;
	m=n=1000;
	A= (double *) malloc( sizeof(double) * m* n);
	xorin = (double *) malloc(sizeof(double) * n);
	noise = (double *) malloc(sizeof(double) * m);
	y = (double *) malloc(sizeof(double) * m);

	state = 234;
	acml_randseed(state);
	acml_randn(A,m*n,0.0,1.0);

	state = 1325;
	acml_randseed(state);
	acml_randn(xorin,n,0.0,1.0);

	state = 33;
	acml_randseed(state);
	acml_randn(noise,m,0.0,1.0);
	dcopy(m,noise,1,y,1);

	dgemv('N',m,n,1.0,A,m,xorin,1,0.01l,y,1);
	fprintM("A.txt",A,1000,1000);
	fprintM("y.txt",y,1000,1);
	rho = 0.1l;


	//result = leastR(A,m,n,y,0.01,0.1,100);
	printf("Enter the value of l2 regression constant:");
	scanf("%lf",&c1);
	printf("Enter the value of l1 regression constant:");
	scanf("%lf",&c2);

	initOPTS(&opt,m,n,0);
//	initNormalization(&opt,A);

	opt.maxIter = 100;
	opt.nFlag = 0;
	opt.rsL2 = c1;
	opt.tFlag = 5;
	opt.rFlag = 0;
	opt.mFlag=0;
	opt.lFlag =0;
	startTimer(&sw);
	result1 = LeastR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	startTimer(&sw);
	opt.mFlag = 1;
	opt.lFlag = 0;
	opt.tFlag = 5;
	result2 = LeastR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	startTimer(&sw);
	opt.mFlag = 1;
	opt.lFlag = 1;
	opt.tFlag = 5;
	result3 = LeastR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));


	fprintM("x1.1.txt",result1.x,1000,1);
	fprintM("funVal1.1.txt",result1.funVal,opt.maxIter,1);
	fprintM("lVal1.1.txt",result1.ValueL,100,1);
	free(result1.x);
	free(result1.funVal);
	free(result1.ValueL);

	fprintM("x1.2.txt",result2.x,1000,1);
	fprintM("funVal1.2.txt",result2.funVal,opt.maxIter,1);
	fprintM("lVal1.2.txt",result2.ValueL,100,1);
	free(result2.x);
	free(result2.funVal);
	free(result2.ValueL);

	fprintM("x1.3.txt",result3.x,1000,1);
	fprintM("funVal1.3.txt",result3.funVal,opt.maxIter,1);
	fprintM("lVal1.3.txt",result3.ValueL,100,1);
	free(result3.x);
	free(result3.funVal);
	free(result3.ValueL);



}


void runLeastR2(){
	int m,n,state;
	double *A, *xorin, *noise, *y, rho;
	OPTS opt;
	double c1,c2;
	stopWatch sw;
	FUNVAL result1,result2,result3;
	m=n=1000;
	A= (double *) malloc( sizeof(double) * m* n);
	xorin = (double *) malloc(sizeof(double) * n);
	noise = (double *) malloc(sizeof(double) * m);
	y = (double *) malloc(sizeof(double) * m);

	state = 234;
	acml_randseed(state);
	acml_randn(A,m*n,0.0,1.0);

	state = 1325;
	acml_randseed(state);
	acml_randn(xorin,n,0.0,1.0);

	state = 33;
	acml_randseed(state);
	acml_randn(noise,m,0.0,1.0);
	dcopy(m,noise,1,y,1);

	dgemv('N',m,n,1.0,A,m,xorin,1,0.01l,y,1);
	fprintM("A.matrix",A,m,n);
	fprintM("y.vector",y,m,1);

	memset(A,0,sizeof(double)*m*n);
	memset(y,0,sizeof(double)*m);
	fscanM("A.matrix",A,m,n);
	fscanM("y.vector",y,m,1);
	
	rho = 0.1l;


	//result = leastR(A,m,n,y,0.01,0.1,100);
	printf("Enter the value of l2 regression constant:");
	scanf("%lf",&c1);
	printf("Enter the value of l1 regression constant:");
	scanf("%lf",&c2);

	initOPTS(&opt,m,n,0);
//	initNormalization(&opt,A);

	opt.maxIter = 100;

	opt.init  = 2;
	opt.tFlag = 5;
	opt.nFlag = 0;
	opt.mFlag = 0;
	opt.lFlag = 0;
	opt.rFlag = 0;
	opt.rsL2 = 0.0;

	startTimer(&sw);
	result1 = LeastR(A,y,c2,opt);
	stopTimer(&sw);
	printf("Time taken = %lf\n",getElapsedTime(&sw));

	fprintM("x.Armijo.vector",result1.x,1000,1);
	fprintM("funVal.Armijo.list",result1.funVal,opt.maxIter,1);
	fprintM("lVal.Armijo.list",result1.ValueL,100,1);
	free(result1.x);
	free(result1.funVal);
	free(result1.ValueL);


}